package com.tjsinfo.tjsoa.fileDocument.bo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.page.UserInfoPage;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentBean;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentFile;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentGroup;

public class FileDocumentFilePageBo implements IFileDocumentFilePageBo {
	private IQbcDao qbcDao;
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getQbcDao()
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getQbcDao()
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFilePageBo#getQbcDao()
	 */
	public IQbcDao getQbcDao() {
		return qbcDao;
	}
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFilePageBo#setQbcDao(com.tjsinfo.tjsoa.Dao.IQbcDao)
	 */
	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
	
	private static int pagecount = 0;
	public int getPagecount() {
		return pagecount;
	}
	
	private static String group = "0";
	public String getGroup() {
		return group;
	}
	
	private static int page = 0;
	public static int getPage() {
		return page;
	}
	public static int pagenum = 3;
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPrivateGotoPage(java.lang.String, int)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getGotoPage(java.lang.String, int, java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFilePageBo#getGotoPage(java.lang.String, int, java.lang.String)
	 */
	public List<FileDocumentBean> getGotoPage(String group_id,int page,String pagenum1,int user_id,String chaxunname,String chaxunren) {
		String groupId = group_id;
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentFile.class);
		if (chaxunname!=null && !"".equals(chaxunname)) {
			criteria.add(Restrictions.like("fileName", "%"+chaxunname+"%"));
		}
		if(chaxunren!=null && !"".equals(chaxunren)){
			DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
			criteria1.add(Restrictions.like("userName", "%"+chaxunren+"%"));
		}
		
		DetachedCriteria criteria2 = criteria.createCriteria("fileDocumentGroup");
		criteria2.add(Restrictions.eq("id", groupId));
		criteria.addOrder(Order.desc("datetime"));
		
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria);
		list = (ArrayList) this.FileExpressToFileBean(list);
		UserInfoPage userPage = new UserInfoPage(list);
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		pagecount = userPage.getPageCount();
		if (page > pagecount) {
			userPage.gotoPage(pagecount);
		}else {
			userPage.gotoPage(page);
		}
		//userPage.setPageIndex(2);
		ArrayList list1 = new ArrayList(userPage.getPageData());
		return list1;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPrivateLastPage(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getLastPage(java.lang.String, java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFilePageBo#getLastPage(java.lang.String, java.lang.String)
	 */
	public List<FileDocumentBean> getLastPage(String group_id,String pagenum1,int user_id,String chaxunname,String chaxunren) {
		int groupId = Integer.valueOf(group_id);
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentFile.class);
		if (chaxunname!=null && !"".equals(chaxunname)) {
			criteria.add(Restrictions.like("fileName", "%"+chaxunname+"%"));
		}
		if(chaxunren!=null && !"".equals(chaxunren)){
			DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
			criteria1.add(Restrictions.like("userName", "%"+chaxunren+"%"));
		}
		
		DetachedCriteria criteria2 = criteria.createCriteria("fileDocumentGroup");
		criteria2.add(Restrictions.eq("id", groupId));
		criteria.addOrder(Order.desc("datetime"));
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria);
		list = (ArrayList) this.FileExpressToFileBean(list);
		UserInfoPage userPage = new UserInfoPage(list);
		
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		page = userPage.getPageCount();
		userPage.gotoPage(userPage.getPageCount());
		pagecount = userPage.getPageCount();
		//userPage.setPageIndex(2);
		ArrayList list1 = new ArrayList(userPage.getPageData());
		return list1;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPrivateFirstPage(int)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getFirstPage(int, java.lang.String, java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFilePageBo#getFirstPage(int, java.lang.String)
	 */
	public List<FileDocumentBean> getFirstPage(int user_id,String pagenum1) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentGroup.class);
		criteria.add(Restrictions.eq("groupName", "我的文档"));
		
		ArrayList<FileDocumentGroup> grouplist = (ArrayList)qbcDao.getCriteria(criteria);
		group = grouplist.iterator().next().getId();
		
		DetachedCriteria criteria2 = DetachedCriteria.forClass(FileDocumentFile.class);
		criteria2.add(Restrictions.and(Restrictions.eq("fileDocumentGroup", grouplist.iterator().next()), Restrictions.like("downUserId", "%,"+user_id+",%")));
		criteria2.addOrder(Order.desc("datetime"));
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria2);
		list = (ArrayList) this.FileExpressToFileBean(list);
		UserInfoPage userPage = new UserInfoPage(list);
		
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		userPage.gotoPage(1);
		pagecount = userPage.getPageCount();
		//userPage.setPageIndex(2);
		ArrayList list1 = new ArrayList(userPage.getPageData());
		return list1;
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.addresslist.AddressList_User.IAddressListUserPageBo#getPageCount(java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileExpress.FileExpress_File.IFileExpressFilePageBo#getPageCount(java.lang.String, java.lang.String)
	 */
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_File.IFileDocumentFilePageBo#getPageCount(java.lang.String, java.lang.String)
	 */
	public int getPageCount(String group_id,String pagenum1,int user_id,String chaxunname,String chaxunren) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentFile.class);
		if (chaxunname!=null && !"".equals(chaxunname)) {
			criteria.add(Restrictions.like("fileName", "%"+chaxunname+"%"));
		}
		if(chaxunren!=null && !"".equals(chaxunren)){
			DetachedCriteria criteria1 = criteria.createCriteria("tjsUser");
			criteria1.add(Restrictions.like("userName", "%"+chaxunren+"%"));
		}
		DetachedCriteria criteria2 = criteria.createCriteria("fileDocumentGroup");
		criteria2.add(Restrictions.eq("id", Integer.valueOf(group_id)));
		ArrayList list = (ArrayList)qbcDao.getCriteria(criteria);
		list = (ArrayList) this.FileExpressToFileBean(list);
		UserInfoPage userPage = new UserInfoPage(list);
		
		if (pagenum1 != null) {
			userPage.setPageSize(Integer.valueOf(pagenum1));
		}else {
			userPage.setPageSize(pagenum);
		}
		return userPage.getPageCount();
	}
	
	private List<FileDocumentBean> FileExpressToFileBean(List list){
		List list2 = new ArrayList();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			FileDocumentFile file = (FileDocumentFile) iterator.next();
			FileDocumentBean bean = new FileDocumentBean();
			bean.setId(file.getId());
			bean.setDatetime(file.getDatetime().toString());
			bean.setSubject(file.getSubject());
			bean.setKeyword(file.getKeyword());
			bean.setDocket(file.getDocket());
			bean.setFileName(file.getFileName());
			bean.setExpressUser(file.getTjsUser().getUserName());
			bean.setFileSize(file.getFileSize());
			bean.setFilePath(file.getFilePath());
			list2.add(bean);
		}
		return list2;
	}
}
